package com.hyb.algorithm.data.struct.stack;

import java.util.Stack;

/**
 * @BelongProject algorithm
 * @BelongPackage com.hyb.algorithm.data.struct.stack
 * @Description TODO
 * @ClassName StackGetMin
 * @Author huyangbing
 * @Date 2025/2/19 17:08
 * @Version 1.0.0
 */
public class StackGetMin {


    private Stack<Integer> mainStack = new Stack<>();
    private Stack<Integer> minStack = new Stack<>();


    /**
     * 入栈操作
     *
     * @param element
     */

    public void push(int element) {

        mainStack.push(element);


        if (minStack.isEmpty() || element <= minStack.peek()) {
            minStack.push(element);
        }
    }


    /***
     *  出栈操作
     */
    public Integer pop() {
        //如果出栈元素和辅助栈顶元素值相等，辅助栈出栈
        if (mainStack.peek().equals(minStack.peek())) {
            minStack.pop();
        }

        return minStack.pop();

    }


    /**
     * 获取栈最小元素
     *
     * @return
     */
    public int getMin() {

        if (mainStack.empty()) {
            throw new IllegalArgumentException(" stack is empty");
        }
        return minStack.peek();
    }

    public static void main(String[] args) {

        StackGetMin stackGetMin = new StackGetMin();
        stackGetMin.push(4);
        stackGetMin.push(9);
        stackGetMin.push(7);
        stackGetMin.push(3);
        stackGetMin.push(8);
        stackGetMin.push(5);

        System.out.println(stackGetMin.getMin());


        stackGetMin.pop();
        System.out.println(stackGetMin.getMin());

    }


}
